"""
Web分析器模块
提供Web界面，用于低位领涨股分析和新闻展示
"""

import os
import sys
import logging

# 配置日志
logger = logging.getLogger(__name__)

# 添加项目根目录到Python路径
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root_dir)

# 从根目录导入web_analyzer
try:
    # Import the app directly
    from flask import Flask, render_template, redirect, url_for, request, jsonify
    import importlib.util
    
    # 尝试加载web_analyzer模块
    spec = importlib.util.spec_from_file_location("web_analyzer", os.path.join(root_dir, "web_analyzer.py"))
    web_analyzer = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(web_analyzer)
    
    # 导出app和news_updater
    app = web_analyzer.app
    news_updater = web_analyzer.news_updater
    
    logger.info("成功从根目录导入web_analyzer")
except (ImportError, AttributeError, FileNotFoundError) as e:
    logger.error(f"无法从根目录导入web_analyzer: {e}")
    # 如果无法导入，创建一个简单的Flask应用
    from flask import Flask, render_template, redirect, url_for, request, jsonify
    
    app = Flask(__name__, 
                template_folder=os.path.join(root_dir, 'templates'),
                static_folder=os.path.join(root_dir, 'static'))
    
    class NewsUpdater:
        def __init__(self):
            self.news_collector = None
            
        def set_news_collector(self, news_collector):
            self.news_collector = news_collector
            
        def get_latest_news(self, limit=10):
            return []
    
    news_updater = NewsUpdater()
    
    @app.route('/')
    def index():
        return render_template('index.html', latest_news=[])
    
    @app.route('/news')
    def news():
        return render_template('news.html', news_list=[])
    
    logger.warning("已创建简单的Flask应用作为后备") 